package com.jianyun.jyzs.dao;

import android.content.Context;
import android.util.Log;


import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.db.DatabaseHelper;
import com.jianyun.jyzs.utils.SysConstant2;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked")
public class UserDao {
    private Dao<User, Integer> userDao;
    private DatabaseHelper helper;

    public UserDao(Context context) {
        try {
            helper = DatabaseHelper.getHelper(context);
//			SQLiteDatabase writableDatabase = helper.getWritableDatabase();
//			helper.onCreate(writableDatabase);
            userDao = helper.getDao(User.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取登录用户
     *
     * @
     */
    public List<User> queryUsers() {
        try {
            return userDao.queryBuilder().where().eq("userType", SysConstant2.NO).query();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<User>();
    }

    /**
     * 获取所有好友
     *
     * @param
     */
    public List<User> queryConstants(String enterpriseCode, String userId) {
        try {
            QueryBuilder<User, Integer> queryBuilder = userDao.queryBuilder();
            List<User> query = queryBuilder.where().eq("userId",userId).and().eq("enterpriseCode",enterpriseCode).query();
            Log.i("test","query:"+query.size());
            return userDao.queryBuilder().where().eq("enterpriseCode", enterpriseCode).and().eq("userId", userId).query();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<User>();
    }

    /**
     * 增加一个用户
     *
     * @param user
     * @
     */
    public void insert(User user) {
        try {
            //User ifNotExists = userDao.createIfNotExists(user);
            Dao.CreateOrUpdateStatus orUpdate = userDao.createOrUpdate(user);
            boolean created = orUpdate.isCreated();
            boolean updated = orUpdate.isUpdated();
            int numLinesChanged = orUpdate.getNumLinesChanged();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    /**
     * 删除一个用户 ById
     *
     * @param user
     * @
     */
    public void deleteById(Integer id) {
        try {
            @SuppressWarnings("rawtypes")
            DeleteBuilder deleteBuilder = userDao.deleteBuilder();
            deleteBuilder.where().eq(User.ID_FIELD_NAME, id);
            deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一个用户 ById
     *
     * @param user
     * @
     */
    public void delete(User user) {
        try {
            userDao.delete(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除所有通讯录
     *
     * @
     */
    public void deleteAllContact(String enterpriseCode, String userId) {
        try {

            @SuppressWarnings("rawtypes")
            DeleteBuilder deleteBuilder = userDao.deleteBuilder();
            deleteBuilder.where().eq("userType", SysConstant2.YES).and().eq("enterpriseCode", enterpriseCode).and().eq("userId", userId);
            deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除通讯录单个用户
     *
     * @
     */
    public void deleteOneContact(String enterpriseCode, String userId, String serverUserId) {
        try {

            @SuppressWarnings("rawtypes")
            DeleteBuilder deleteBuilder = userDao.deleteBuilder();
            deleteBuilder.where().eq("userType", SysConstant2.YES).and().eq("enterpriseCode", enterpriseCode).and().eq("userId", userId);
            deleteBuilder.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改一个用户
     *
     * @param user
     * @
     */
    public void update(User user) {
        try {
            userDao.createOrUpdate(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改登录用户状态为未登陆
     *
     * @return
     * @
     */
    public void updateState() {
        StringBuffer sql = new StringBuffer("");
        sql.append("update ");
        sql.append(User.TABLE_NAME);
        sql.append(" set loginState = " + SysConstant2.NO);
        sql.append(" where userType = " + SysConstant2.NO);
        try {
            userDao.executeRaw(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询当前登录的用户
     *
     * @return
     * @
     */
    public User findLoginUser(int loginStatus) {
        try {
            return userDao.queryBuilder().where().eq("loginState", loginStatus).and().eq("userType", SysConstant2.NO).queryForFirst();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Id的用户
     *
     * @return
     * @
     */
    public User findUserByID(Integer id) {
        try {
            return userDao.queryBuilder().where().eq(User.ID_FIELD_NAME, id).queryForFirst();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * serverUserId的用户
     *
     * @return
     * @
     */
    public User findUserByID(String serverId, String userId, String enterpriseCode) {
        try {
            return userDao.queryBuilder().where().eq("serverUserId", serverId).and().eq("userId", userId).and().eq("enterpriseCode", enterpriseCode).queryForFirst();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * serverUserId的用户
     *
     * @return
     * @
     */
    public User findUserByID(String serverId, String userId, int type, String enterpriseCode) {
        try {
            return userDao.queryBuilder().where().eq("serverUserId", serverId).and().eq("userId", userId).and().eq("enterpriseCode", enterpriseCode).and().eq("userType", type).queryForFirst();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String[] getUserIconByUserID(Integer id) {
        String[] str = new String[2];
        try {
            User user = userDao.queryBuilder().where().eq(User.ID_FIELD_NAME, id).queryForFirst();
            if (user != null) {
                str[0] = user.getLocalIconPath();
                str[1] = user.getServerIconPath();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public String[] getUserIconByUserID(String serverUserId, String userId, String enterpriseCode) {
        String[] str = new String[2];
        try {
            if (serverUserId == null || userId == null) {
                return str;
            }
            User user = userDao.queryBuilder().where().eq("serverUserId", serverUserId).and().eq("userId", userId).and().eq("enterpriseCode", enterpriseCode).queryForFirst();
            if (user != null) {
                str[0] = user.getLocalIconPath();
                str[1] = user.getServerIconPath();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public User getIdByOtherID(String myId, String otherId, String enterpriseCode) {
        try {
            return userDao.queryBuilder().where().eq("userId", myId).and().eq("serverUserId", otherId).and().eq("enterpriseCode", enterpriseCode).queryForFirst();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}